package mahan.test.data.mongodb;

import static org.junit.Assert.assertNotNull;

import java.util.Set;

import mahan.data.mongodb.MahanDB;

import org.junit.Test;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

public class DBTestCase {
	
	@Test
	public void testDB() throws Exception
	{
		MahanDB mahanDb = new MahanDB("localhost", 27017, "mydb" );
		DB db = mahanDb.get();
		assertNotNull(db);
		db.dropDatabase();
		
		//Create a collection
		DBCollection testCollection = mahanDb.createCollection("TestCollection");
		//Insert something into the collection
		BasicDBObject doc = new BasicDBObject();

        doc.put("name", "MongoDB");
        doc.put("type", "database");
        doc.put("count", 1);

        BasicDBObject info = new BasicDBObject();

        info.put("x", 203);
        info.put("y", 102);

        doc.put("info", info);

        testCollection.insert(doc);
        
        DBObject myDoc = testCollection.findOne();
        Set<String> keys = myDoc.keySet();
        assertNotNull(keys);
        for( String key: keys)
        {
        	Object obj = myDoc.get(key);
        	System.out.println( "Key=" + key + "::" + obj);
        }
        db.dropDatabase();
	}
}